package com.jessechanrui.template.common.template.auth.websocket;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

/**
 * @author chenrui
 * @version 1.0
 * @description ChatWebSocketHandler
 * @date 2025/8/12
 */
@Component
@Slf4j
public class ChatWebSocketHandler extends WebSocketHandler{

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String path() {
        return "/ws/chat";
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String payload = message.getPayload();
        log.info("chat request: {}", payload);
        ChatRequest request = objectMapper.readValue(payload, ChatRequest.class);
        session.sendMessage(new TextMessage("响应:" + payload));
    }
}
